/*
	使用promise 封装uni.request
*/

//封装uni.request
const http = (url, data = {}, method = 'get') => {
	return new Promise((reslove, reject) => {
		uni.request({
			url,
			data,
			method,
			// header:{
			// 	'custom-header':'hello' //自定义请求头部信息
			// },
			success: (res) => {
				reslove(res.data)
			},
			fail: (error) => {
				reject(error)
			}
		})
	})
}

//再次封装get请求
const get = (url, data = {}) => http(url, data, 'get')
//再次封装post请求
const post = (url, data = {}) => http(url, data, 'post')

export default {
	http,
	get,
	post
}

//请求拦截器
uni.addInterceptor('request', {
  invoke(args) {
    // request 触发前拼接 url 
    args.url = process.env.VUE_APP_BASE_URL+args.url
  },
  success(args) {
    // 请求成功后，修改code值为1
    args.data.code = 1
  }, 
  fail(err) {
    console.log('interceptor-fail',err)
  }, 
  complete(res) {
    console.log('interceptor-complete',res)
  }
})

uni.addInterceptor({
  returnValue(args) {
    // 只返回 data 字段
    return args.data
  }
})
